import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/views/cooperation/cooperation_page.dart';
import 'package:xinxuan/views/home/home_page.dart';
import 'package:xinxuan/views/my/my_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/views/service/service_page.dart';

class CommonPage extends StatelessWidget {
  
  final CommonPageController commonPageController = Get.find();

  /// 对应页面
  final List<Widget> _mainPage = [
    HomePage(), 
    CooperationPage(), 
    ServicePage(), 
    MyPage()
  ];

  /// 底部栏
  /// 底部导航栏
  List<Widget> _buildTabItemArr(int index) {
    List<Widget> widges = [
      _tabItem(
        index: 0,
        iconPath: index == 0 ? 'assets/icons/icon_common_home_selected.png' : 'assets/icons/icon_common_home.png', 
        title: '首页'
      ),
      _tabItem(
        index: 1,
        iconPath: index == 1 ? 'assets/icons/icon_cooperation_selected.png' : 'assets/icons/icon_cooperation.png', 
        title: '合作'
      ),
      Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            width: 24.w,
            height: 24.w,
          ),
          Text(
            '抢购',
            style: TextStyle(
              fontSize: 12.sp,
              color: textSubtitleColor
            ),
          )
        ],
      ),
      _tabItem(
        index: 2,
        iconPath: index == 2 ? 'assets/icons/icon_service_selected.png' : 'assets/icons/icon_service.png', 
        title: '客服'
      ),
      _tabItem(
        index: 3,
        iconPath: index == 3 ? 'assets/icons/icon_common_me_selected.png' : 'assets/icons/icon_common_me.png',
        title: '我的'
      ),
    ];
    return widges;
  }

  /// 导航栏Item
  Widget _tabItem({required int index, required String iconPath, required String title}) {
    return InkWellUtil.shakeProofInkWell(Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            iconPath,
            width: 24.w,
            height: 24.w
          ),
          Text(
            title,
            style: TextStyle(
              fontSize: 12.sp,
              color: textSubtitleColor
            ),
          )
        ],
      ),
    ), () {
      commonPageController.tabIndex.value = index;
      if (index == 1 || index == 3) {
        commonPageController.checkLogin(loading: false);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // commonPageController.checkAppVersion(showLoading: false);
    return WillPopScope(
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        body: Obx(() => IndexedStack(
          index: commonPageController.tabIndex.value,
          children: _mainPage,
        )),
        bottomNavigationBar: BottomAppBar(
          // shape: CircularNotchedRectangle(),
          child: Container(
            height: 56.h,
            child: Obx(() => Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: _buildTabItemArr(commonPageController.tabIndex.value),
            )),
          )
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.transparent,
          elevation: 0,
          focusElevation: 0,
          hoverElevation: 0,
          disabledElevation: 0,
          highlightElevation: 0,
          onPressed: () {
            Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_PAGE}');
          },
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(100.w),
              border: Border.all(
                color: bgWhiteColor,
                width: 3.w
              )
            ),
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(100.w),
                boxShadow: [
                  BoxShadow(
                    color: bgMainColor_4,
                    offset: Offset(0, 4.h),
                    blurRadius: 6.w,            
                  )
                ]
              ),
              child: Image.asset(
                'assets/icons/icon_seckill.png',
                fit: BoxFit.cover,
                width: 48.w,
                height: 48.w,
              ),
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      ), 
      onWillPop: () {
        if(lastPopTime == null || DateTime.now().difference(lastPopTime ??  DateTime.now()) > Duration(seconds: 2)){
          lastPopTime = DateTime.now();
          EasyLoading.showToast('再按一次退出程序');
          return Future.value(false);
        } else {
          lastPopTime = DateTime.now();
          // 退出app
          SystemChannels.platform.invokeMethod('SystemNavigator.pop');
          return Future.value(true);
        }
      }
    );
  }
}
